package com.leetcode.algorithm.y22.m09.w4;

/**
 * 1021. 删除最外层的括号
 * 
 * https://leetcode.cn/problems/remove-outermost-parentheses/
 * 
 * @author jie.deng
 *
 */
class Question1021Solution01 {

	public String removeOuterParentheses(String s) {
		StringBuilder builder = new StringBuilder();

		int len = s.length();
		int left = 0;
		int size = 1;
		int idx = left + 1;
		while (idx < len) {
			char ch = s.charAt(idx);
			if (ch == '(') {
				size++;
			} else {
				size--;
				if (size == 0) {
					builder.append(s.substring(left + 1, idx));
					idx++;
					if (idx < len) {
						left = idx;
						size = 1;
					}
				}
			}
			idx++;
		}

		return builder.toString();
	}

}